//
//  OrderListTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/21.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "OrderListTableViewCell.h"

@interface OrderListTableViewCell()
@property (nonatomic, strong)UIView * whiteBGView;
@property (nonatomic, strong)UILabel * dateLab;
@property (nonatomic, strong)UILabel * stateLab;
@property (nonatomic, strong)UIView * lineView;
@property (nonatomic, strong)UIImageView * produceImg;
@property (nonatomic, strong)UILabel * contentLab;
@property (nonatomic, strong)UILabel * moneyLab;
@property (nonatomic, strong)NSString * myListID;
@property (nonatomic, strong)UILabel * produceID;
@property (nonatomic, strong)UIButton * refundBtn;//退款
@property (nonatomic, strong)UILabel * produceState;//首饰：已发货、未发货     酒店等：已消费、未消费
@end

@implementation OrderListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = getColor(mainColor);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.whiteBGView];
        [self.contentView addSubview:self.dateLab];
        [self.contentView addSubview:self.stateLab];
        [self.contentView addSubview:self.lineView];
        [self.contentView addSubview:self.produceImg];
        [self.contentView addSubview:self.contentLab];
        [self.contentView addSubview:self.moneyLab];
        [self.contentView addSubview:self.refundBtn];
        [self.contentView addSubview:self.produceState];
        __weak typeof (self)weakSelf = self;
        [self.whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.contentView);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10/WIDTH_6_SCALE);
        }];
        [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(20/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteBGView.mas_top);
            make.bottom.equalTo(weakSelf.lineView.mas_top);
        }];
        [self.stateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_right).mas_offset(-51/WIDTH_6_SCALE);
            make.centerY.top.equalTo(weakSelf.dateLab);
        }];
        [self.refundBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.whiteBGView.mas_top).mas_offset(2/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.lineView.mas_top).mas_offset(-2/WIDTH_6_SCALE);
            make.width.mas_offset(65/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-10/WIDTH_6_SCALE);
        }];
        [self.produceState mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(weakSelf.lineView.mas_bottom).mas_offset(2/WIDTH_6_SCALE);
            make.width.height.equalTo(weakSelf.refundBtn);
            make.left.equalTo(weakSelf.contentView.mas_right).mas_offset(-51/WIDTH_6_SCALE);
            make.centerY.equalTo(weakSelf.produceImg.mas_centerY);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.whiteBGView.mas_top).mas_offset(33/WIDTH_6_SCALE);
            make.left.right.equalTo(weakSelf.whiteBGView);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        [self.produceImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.lineView.mas_bottom).mas_offset(10/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(15/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.whiteBGView.mas_bottom).mas_offset(-22/WIDTH_6_SCALE);
            make.width.mas_offset(55/WIDTH_6_SCALE);
        }];
        [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.produceImg.mas_right).mas_offset(25/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.lineView.mas_bottom).mas_offset(16/WIDTH_6_SCALE);
        }];
        [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentLab.mas_left);
            make.bottom.equalTo(weakSelf.whiteBGView.mas_bottom).mas_offset(-17/WIDTH_6_SCALE);
        }];
    }return self;
}
- (UIView *)whiteBGView{
    if (!_lineView) {
        _whiteBGView = [[UIView alloc] init];
        _whiteBGView.backgroundColor = [UIColor whiteColor];
    }return _whiteBGView;
}
- (UILabel *)dateLab{
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] init];
        _dateLab.font = DEF_FontSize_10;
        _dateLab.textColor = [UIColor blackColor];
        _dateLab.textAlignment = NSTextAlignmentLeft;
        _dateLab.text = @"xxxx/xx/xx";
    }return _dateLab;
}
- (UILabel *)stateLab{
    if (!_stateLab) {
        _stateLab = [[UILabel alloc] init];
        _stateLab.font = DEF_FontSize_12;
        _stateLab.textColor = getColor(mainRedColor);
        _stateLab.textAlignment = NSTextAlignmentLeft;
        _stateLab.text = @"---";
    }return _stateLab;
}
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = getColor(mainColor);
    }return _lineView;
}
- (UIImageView *)produceImg{
    if (!_produceImg) {
        _produceImg = [[UIImageView alloc] init];
        _produceImg.image = [UIImage imageNamed:@"YimiPlaceHolder"];
    }return _produceImg;
}
- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] init];
        _contentLab.font = DEF_FontSize_14;
        _contentLab.textColor = [UIColor blackColor];
        _contentLab.textAlignment = NSTextAlignmentLeft;
        _contentLab.numberOfLines = 0;
        _contentLab.lineBreakMode = 0;
        _contentLab.text = @"---";
    }return _contentLab;
}
- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.font = DEF_FontSize_12;
        _moneyLab.textColor = [UIColor blackColor];
        _moneyLab.textAlignment = NSTextAlignmentLeft;
        _moneyLab.text = [NSString stringWithFormat:@"总价：￥---"];
    }return _moneyLab;
}
- (UIButton *)refundBtn{
    if (!_refundBtn) {
        _refundBtn = [[UIButton alloc] init];
        _refundBtn.titleLabel.font = DEF_FontSize_12;
        [_refundBtn setTitle:@"申请退款" forState:UIControlStateNormal];
        [_refundBtn setBackgroundColor:[UIColor whiteColor]];
        [_refundBtn addTarget:self action:@selector(refundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_refundBtn setTitleColor:getColor(mainRedColor) forState:UIControlStateNormal];
        _refundBtn.layer.borderColor = getColor(mainRedColor).CGColor;
        _refundBtn.layer.borderWidth = 1;
        _refundBtn.layer.cornerRadius = 4;
        _refundBtn.layer.masksToBounds = YES;
        _refundBtn.hidden = NO;
    }return _refundBtn;
}
- (UILabel *)produceState{
    if (!_produceState) {
        _produceState = [[UILabel alloc] init];
        _produceState.font = DEF_FontSize_12;
        _produceState.textColor = getColor(mainRedColor);
        _produceState.textAlignment = NSTextAlignmentLeft;
        _produceState.text = @"---";
    }return _produceState;
}
- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.produceState.hidden = NO;
    self.dateLab.text = [self ConvertStrToTime:model[@"createtime"]];
    self.contentLab.text = model[@"goodsname"];
    self.moneyLab.text = [NSString stringWithFormat:@"总价：￥%@", model[@"totalmoney"]];
    [self.produceImg sd_setImageWithURL:[NSURL URLWithString:model[@"goodsimg"]] placeholderImage:[UIImage imageNamed:@""]];
    //refund:1、可退款  0、不可退款
    self.refundBtn.userInteractionEnabled = YES;
    if ([model[@"refund"] isEqualToString:@"1"] && [model[@"paystatus"] isEqualToString:@"1"]) {
        self.refundBtn.hidden = NO;
        self.stateLab.hidden = YES;
        [self.refundBtn setTitle:@"申请退款" forState:UIControlStateNormal];
    }else if ([model[@"paystatus"] isEqualToString:@"0"]){
        self.refundBtn.hidden = YES;
        self.stateLab.hidden = NO;
        self.stateLab.text = @"未支付";
        self.produceState.hidden = YES;
    }else if ([model[@"paystatus"] isEqualToString:@"1"]){
        self.refundBtn.hidden = YES;
        self.stateLab.hidden = NO;
        self.stateLab.text = @"已支付";
    }else if ([model[@"paystatus"] isEqualToString:@"2"]){
        self.refundBtn.hidden = YES;
        self.stateLab.hidden = NO;
        self.stateLab.text = @"审核中";
    }else if ([model[@"paystatus"] isEqualToString:@"3"]){
        self.refundBtn.hidden = YES;
        self.stateLab.hidden = NO;
        self.stateLab.text = @"已退款";
    }else if ([model[@"paystatus"] isEqualToString:@"4"]){
        self.refundBtn.hidden = YES;
        self.stateLab.hidden = NO;
        self.stateLab.text = @"已拒绝";
    }else if ([model[@"paystatus"] isEqualToString:@"5"]){
        self.refundBtn.hidden = NO;
        self.stateLab.hidden = YES;
        [self.refundBtn setTitle:@"审核通过" forState:UIControlStateNormal];
    }else if ([model[@"paystatus"] isEqualToString:@"6"]){
        self.refundBtn.hidden = NO;
        [self.refundBtn setTitle:@"退款中" forState:UIControlStateNormal];
        self.refundBtn.userInteractionEnabled = NO;
    }
    if ([model[@"businid"] isEqualToString:@"1"]) {
        if ([model[@"sendstatus"] isEqualToString:@"1"]) {
            self.produceState.text = @"已发货";
        }else{
            self.produceState.text = @"未发货";
        }
    }else{
        if ([model[@"usetype"] isEqualToString:@"0"]) {
            self.produceState.text = @"未使用";
        }else{
            self.produceState.text = @"已使用";
        }
    }
    self.produceID = model[@"goodsid"];
    self.myListID = model[@"orderid"];
}
- (void)refundBtnClick:(UIButton *)sendre{
    if ([self.model[@"refund"] isEqualToString:@"1"] && [self.model[@"paystatus"] isEqualToString:@"1"]) {
        if (self.refundSupplyBlock) {//申请退款
            self.refundSupplyBlock(self.myListID);
        }
    }else{
        if (self.refundApproveBlock) {//申请通过
            self.refundApproveBlock(self.myListID);
        }
    }
    
}
//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
}

@end
